package pinboard.dataLayer.potocol.op;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import pinboard.dataLayer.ByteArrayConvert;
import pinboard.dataLayer.RefBag;
import pinboard.dataLayer.binPack.DefaultBinSerializer;
import pinboard.dataLayer.potocol.Guid;
import pinboard.dataLayer.potocol.renderFormat.share.Point;
import pinboard.dataLayer.potocol.renderFormat.share.Stroke;

//Stroke对象没按常规方式序列化，所以只好自定义这个类的序列化器来处理算法。要是稍微修改其实就可以省去这100行代码
public class StrokeBinSerializer extends DefaultBinSerializer {

    static GuidBinSerializer guidBinSerializer = new GuidBinSerializer();

    @Override
    public byte[] toBytes(Object input) {
        Stroke stroke = (Stroke) input;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try {

            baos.write(guidBinSerializer.toBytes(stroke.id));
            baos.write(ByteArrayConvert.int2Bytes(stroke.color));
            baos.write(ByteArrayConvert.double2Bytes(stroke.width));
            baos.write(ByteArrayConvert.double2Bytes(stroke.height));
            baos.write(stroke.isHighlighter);
            baos.write(stroke.fitToCurve);

            baos.write(ByteArrayConvert.int2Bytes(stroke.shapeMode));
            baos.write(ByteArrayConvert.int2Bytes(stroke.fillColor));
            for (Point pt : stroke.points) {
                baos.write(ByteArrayConvert.float2Bytes(pt.x));
                baos.write(ByteArrayConvert.float2Bytes(pt.y));
            }
            byte[] content = baos.toByteArray();
            result.write(ByteArrayConvert.int2Bytes(content.length));
            result.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toByteArray();
    }

    @Override
    public Object toObject(byte[] input, RefBag<Integer> offset) {
        Stroke stroke = new Stroke();

        int allLen = ByteArrayConvert.bytes2Int(input, offset.data);
        offset.data = offset.data + 4;

        int initOffset = offset.data;//记录初始

        stroke.id = (Guid) guidBinSerializer.toObject(input, offset);

        stroke.color = ByteArrayConvert.bytes2Int(input, offset.data);
        offset.data = offset.data + 4;

        stroke.width = ByteArrayConvert.bytes2Double(input, offset.data);
        offset.data = offset.data + 8;

        stroke.height = ByteArrayConvert.bytes2Double(input, offset.data);
        offset.data = offset.data + 8;

        stroke.isHighlighter = input[offset.data];
        offset.data = offset.data + 1;

        stroke.fitToCurve = input[offset.data];
        offset.data = offset.data + 1;

        stroke.shapeMode = ByteArrayConvert.bytes2Int(input, offset.data);
        offset.data = offset.data + 4;

        stroke.fillColor = ByteArrayConvert.bytes2Int(input, offset.data);
        offset.data = offset.data + 4;

        //就是这里缺少了数组长度，导致没按基本规则序列化
        int remainLen = allLen - (offset.data - initOffset);//计算剩余内容长度，全部解析为points
        int pointCount = remainLen / 8;//每个point的byte数量为8

        stroke.points = new Point[pointCount];
        for (int i = 0; i < pointCount; i++) {
            Point pt = new Point();
            pt.x = ByteArrayConvert.bytes2Float(input, offset.data);
            offset.data = offset.data + 4;

            pt.y = ByteArrayConvert.bytes2Float(input, offset.data);
            offset.data = offset.data + 4;
            stroke.points[i] = pt;
        }
        return stroke;
    }

    @Override
    public Object[] toArray(byte[] input, RefBag<Integer> offset) {
        return super.toArray(input, offset, Stroke.class);
    }
}
